﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculateModelClasses;
using System.IO;
using RayTracingModel;
using UanFileProceed;

namespace FileOutput
{
    public class PathOutput : P2mFileOutput
    {

        /// <summary>
        /// 输出P2M格式的path文件
        /// </summary>
        /// <param name="gridPath">一个发射机和一组接收机所有的路径信息</param>
        /// <param name="Rxname">接收机的名字(点接收机还是区域接收机)</param>
        /// <param name="groupnum">这组接收机是第几组</param>
        public static void OutPath(List<List<CalculateModelClasses.Path>> Paths, string sPath, string projectName)//需要提供一个接受区域所有路径的信息<加上输入路径参数和工程名>
        {
            string Rxname = Paths[0][0].node[Paths[0][0].node.Count - 1].name;
            string groupnum = Paths[0][0].node[Paths[0][0].node.Count - 1].num.ToString();
            if (Paths[0][0].node[Paths[0][0].node.Count - 1].num < 10)
            {
                groupnum = "0" + groupnum;
            }
            FileStream fs = new FileStream(sPath + projectName + ".paths" + ".t001_0" + Paths[0][0].Txnum + ".r0" + groupnum + ".p2m", FileMode.Create);//如果是接收区域的话，多个接收点组成一组接收机组成一个编号
            StreamWriter sw = new StreamWriter(fs);
            for (int i = 0; i < Paths.Count; i++)
            {
                OutEveryRxPath(Paths[i], Rxname, Paths.Count);
            }
            sw.Write(sb);
            sb.Clear();
            sw.Close();
            fs.Close();
        }
        public static void OutPath1(List<List<CalculateModelClasses.Path>> Paths, string sPath)
        {
            FileStream fs = new FileStream(sPath + @"PathOutputNull.p2m", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.Close();
            fs.Close();
        }
        private static void Direct(CalculateModelClasses.Path path)
        {

            sb.AppendLine("Tx-Rx");
            string tempx1 = path.node[0].position.x.ToString("0.#####E+00");
            string tempy1 = path.node[0].position.y.ToString("0.#####E+00");
            string tempz1 = path.node[0].position.z.ToString("f3");
            string tempx2 = path.node[path.node.Count - 1].position.x.ToString("0.#####E+00");
            string tempy2 = path.node[path.node.Count - 1].position.y.ToString("0.#####E+00");
            string tempz2 = path.node[path.node.Count - 1].position.z.ToString("f3");
            if (path.node[0].position.x > 0)
            {
                tempx1 = " " + tempx1;
            }
            if (path.node[0].position.y > 0)
            {
                tempy1 = " " + tempy1;
            }
            if (path.node[0].position.z > 0)
            {
                tempz1 = " " + tempz1;
            }
            if (path.node[path.node.Count - 1].position.x > 0)
            {
                tempx2 = " " + tempx2;
            }
            if (path.node[path.node.Count - 1].position.x > 0)
            {
                tempy2 = " " + tempy2;
            }
            if (path.node[path.node.Count - 1].position.x > 0)
            {
                tempz2 = " " + tempz2;
            }
            sb.AppendLine(" " + tempx1 + "   " + tempy1 + "       " + tempz1);
            sb.AppendLine(" " + tempx2 + "   " + tempy2 + "       " + tempz2);

        }
        private static void NotDirect(CalculateModelClasses.Path path)
        {

            if (path.node.Count == 3)
            {
                sb.AppendLine("Tx-G-Rx ");
                foreach (PathNode node in path.node)
                {
                    string tempx = node.position.x.ToString("0.#######E+00");
                    string tempy = node.position.y.ToString("0.#######E+00");
                    string tempz = node.position.z.ToString("f3");
                    if (node.position.x > 0)
                    {
                        tempx = " " + tempx;
                    }
                    if (node.position.y > 0)
                    {
                        tempy = " " + tempy;
                    }
                    if (node.position.z > 0)
                    {
                        tempz = " " + tempz;
                    }
                    sb.AppendLine(tempx + "   " + tempy + "       " + tempz);
                }
            }
            if (path.node.Count == 4)
            {
                sb.AppendLine("Tx-G-G-Rx ");
                foreach (PathNode node in path.node)
                {
                    string tempx = node.position.x.ToString("0.#######E+00");
                    string tempy = node.position.y.ToString("0.#######E+00");
                    string tempz = node.position.z.ToString("f3");
                    if (node.position.x > 0)
                    {
                        tempx = " " + tempx;
                    }
                    if (node.position.y > 0)
                    {
                        tempy = " " + tempy;
                    }
                    if (node.position.z > 0)
                    {
                        tempz = " " + tempz;
                    }
                    sb.AppendLine(tempx + "   " + tempy + "       " + tempz);
                }
            }
            if (path.node.Count == 5)
            {
                sb.AppendLine("Tx-G-G-G-Rx ");
                foreach (PathNode node in path.node)
                {
                    string tempx = node.position.x.ToString("0.#######E+00");
                    string tempy = node.position.y.ToString("0.#######E+00");
                    string tempz = node.position.z.ToString("f3");
                    if (node.position.x > 0)
                    {
                        tempx = " " + tempx;
                    }
                    if (node.position.y > 0)
                    {
                        tempy = " " + tempy;
                    }
                    if (node.position.z > 0)
                    {
                        tempz = " " + tempz;
                    }
                    sb.AppendLine(tempx + "   " + tempy + "       " + tempz);
                }
            }

        }

        private static void OutEveryRxPath(List<CalculateModelClasses.Path> paths, string Rxname, int Rxnum)//接收机的名字,加到path里面,Rxnum接收机的个数
        {



            sb.AppendLine("# Receiver Set:" + Rxname);
            sb.AppendLine("     " + Rxnum);//Rxnum是这一组有多少个接收机
            for (int i = 1; i <= Rxnum; i++)
            {
                sb.AppendLine("     " + i + "     " + paths.Count);
                if (paths.Count != 0)
                {


                    string powertemp = Math.Sqrt(Math.Pow(P2mFileOutput.GetTolPower(paths).Re, 2) + Math.Pow(P2mFileOutput.GetTolPower(paths).Im, 2)).ToString("f2");
                    sb.AppendLine(powertemp + "   " + P2mFileOutput.GetTolTime(paths).ToString("0.#####E+00") + "  " + P2mFileOutput.GetTolDelay(paths).ToString("0.#####E+00"));
                    for (int j = 0; j < paths.Count; j++)
                    {
                        double distance = 0;
                        string theta_a = ReadUan.GetThetaAngle(paths[j].node[0].position, paths[j].node[1].position).ToString("f4");
                        string phi_a = ReadUan.GetPhiAngle(paths[j].node[0].position, paths[j].node[1].position).ToString("f4");
                        string theta_d = ReadUan.GetThetaAngle(paths[j].node[paths[j].node.Count - 2].position, paths[j].node[paths[j].node.Count - 1].position).ToString("f4");
                        string phi_d = ReadUan.GetPhiAngle(paths[j].node[paths[j].node.Count - 2].position, paths[j].node[paths[j].node.Count - 1].position).ToString("f4");

                        for (int k = 0; k < paths[j].node.Count - 1; k++)
                        {
                            distance += Intersection.Distance(paths[j].node[k].position, paths[j].node[k + 1].position);
                        }
                        string temp2 = Math.Sqrt(Math.Pow(paths[j].node[0].power.Re, 2) + Math.Pow(paths[j].node[0].power.Im, 2)).ToString("f4");
                        sb.AppendLine("    " + (j + 1) + "   " + (paths.Count - 2).ToString() + "     " + temp2 + "   " + (30 / distance).ToString("0.#####E+00") + "    " + theta_a + "   " + phi_a + "   " + theta_d + "   " + phi_d + "   ");//达到时间需要计算，另外四个角度需要计算"(theta,phia,thetd,phid")
                        if (paths[j].node.Count == 2)
                        {
                            Direct(paths[j]);
                        }
                        else
                        {
                            NotDirect(paths[j]);
                        }
                    }
                }
            }
        }


    }
}
